/**
  ******************************************************************************
  * File Name          : storage.c
  * Description        : Code for storage applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "tskcfg.h"
#include "bsp.h"
#include "storage.h"
#include "spiflash.h"
#include "syscfg.h"
#include "ctrl_common.h"

TaskHandle_t xStoragePara_Thd = NULL;
StaticTask_t xStoragePara_Tcb;
StackType_t  xStoragePara_Stk[ STGPARA_STACK_SIZE ];

QueueHandle_t        Storage_qhd = NULL;
static StaticQueue_t storage_stru;
static uint8_t       storage_area[STORAGE_QUE_LENGTH  * STORAGE_QUE_ITEMSIZE];

void Storage_Parameter(void)
{
	sFlash_Init();
	
	if(SysCfg_LoadPara() != 0)
	{
		SysUsrLog("SysCfg check error\r\n");
	}
	
  if(SysRil_LoadPara() != 0)
	{
		SysUsrLog("SysRil check error\r\n");
	}
  
	if(Ctr_LoadMDPara() != 0)
	{
		SysUsrLog("MD check error\r\n");
	}
	
	if(Ctr_LoadJTPara() != 0)
	{
		SysUsrLog("JT check error\r\n");
	}
	
	if(Ctr_LoadDL1Para() != 0)
	{
		SysUsrLog("DL1 check error\r\n");
	}
	
  if(Ctr_LoadDL2Para() != 0)
	{
		SysUsrLog("DL2 check error\r\n");
	}
  
	if(Ctr_LoadSKPara() != 0)
	{
		SysUsrLog("SK check error\r\n");
	}
	
	if(Ctr_LoadTDPara() != 0)
	{
		SysUsrLog("TD check error\r\n");
	}
  
  if(Ctr_LoadDKPara() != 0)
	{
		SysUsrLog("DK check error\r\n");
	}
	
	SysUsrLog("\r\n");
	SysUsrLog("*****************************\r\n");
	SysUsrLog("Edct LoRa   : %d\r\n", SysCfg_RunPara.lora_num);
	SysUsrLog("Edct RS485  : %d\r\n", SysCfg_RunPara.rs485_num);
	SysUsrLog("*****************************\r\n");
}

void vStoragePara_Task( void * pvParameters )
{
	Storage_SaveType sftype;

	Storage_qhd = xQueueCreateStatic(STORAGE_QUE_LENGTH, STORAGE_QUE_ITEMSIZE,
	                                storage_area, &storage_stru);
  
  Route_CheckDevlist();
  
	while(1)
	{
		if(xQueueReceive(Storage_qhd, &sftype, portMAX_DELAY) == pdTRUE)
		{
			switch(sftype)
			{
				case SFLASH_SAVE_ROUTEPARA:
					Route_SaveDevlist();
					break;
				
				case SFLASH_SAVE_SYSCFG:
					SysCfg_SavePara();
					break;
				
        case SFLASH_SAVE_SYSRIL:
					SysRil_SavePara();
					break;
        
				case SFLASH_SAVE_CTRMD:
					Ctr_SaveMDPara();
					break;
				
				case SFLASH_SAVE_CTRJT:
					Ctr_SaveJTPara();
					break;
				
				case SFLASH_SAVE_CTRDL1:
					Ctr_SaveDL1Para();
					break;
        
        case SFLASH_SAVE_CTRDL2:
					Ctr_SaveDL2Para();
					break;
				
				case SFLASH_SAVE_CTRSK:
					Ctr_SaveSKPara();
					break;
				
				case SFLASH_SAVE_CTRTD:
					Ctr_SaveTDPara();
					break;
        
        case SFLASH_SAVE_CTRDK:
					Ctr_SaveDKPara();
					break;
        
				default:
					break;
			}
		}
	}
}
